package com.miyake.loveviewweb.client.account;

import com.google.appengine.api.datastore.Key;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.miyake.loveviewweb.client.service.DataService;
import com.miyake.loveviewweb.client.service.DataServiceAsync;
import com.miyake.loveviewweb.shared.model.UserModel;
import com.google.gwt.user.client.ui.Label;

public class CreateAccount extends Composite implements HasText {

    private static UserInformationUiBinder uiBinder = GWT
        .create(UserInformationUiBinder.class);
    @UiField TextBox textEmail;
    @UiField TextBox textFirstName;
    @UiField Button button;
    @UiField Label lbl;
    @UiField TextBox textLastName;
    private CreateAccountListener listener;

    interface UserInformationUiBinder extends UiBinder<Widget, CreateAccount> {
    }

    public CreateAccount(CreateAccountListener listener, String email, String nickname) {
        initWidget(uiBinder.createAndBindUi(this));
        this.listener = listener;
        this.textEmail.setText(email);
        this.textFirstName.setText(nickname);
        initialize();
    }

    public CreateAccount(String firstName) {
        initWidget(uiBinder.createAndBindUi(this));
        initialize();
    }

    private void initialize() {

    }

    public void setText(String text) {

    }

    public String getText() {
        return "";
    }

    @UiHandler("button")
    void onButtonClick(ClickEvent event) {
        DataServiceAsync service = DataService.Util.getInstance();
        UserModel prop = new UserModel();
        prop.setEmail(this.textEmail.getText());
        prop.setFirstName(this.textFirstName.getText());
        prop.setLastName(this.textLastName.getText());
        service.createUser(prop, new AsyncCallback<Key>() {

            public void onFailure(Throwable caught) {
                Window.alert(caught.getMessage());
            }

            public void onSuccess(Key result) {
                listener.onCreateUser();
            }
            
        });
        
    }
}
